package com.ruoyi.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.domain.*;
import com.ruoyi.dto.*;
import com.ruoyi.framework.web.service.OnlineUserService;
import com.ruoyi.mapper.*;
import com.ruoyi.service.IBasisCaseLoadingService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 案例加载Service业务层处理
 * 
 * @author wangzebing
 * @date 2025-05-11
 */
@Service
public class BasisCaseLoadingServiceImpl implements IBasisCaseLoadingService
{
    @Autowired
    private BasisCaseLoadingMapper basisCaseLoadingMapper;

    @Autowired
    private BasisCampMapper basisCampMapper;

    @Autowired
    private BaseEqInfoMapper baseEqInfoMapper;

    @Autowired
    private BasisPositionMapper basisPositionMapper;

    @Autowired
    private BasisUavMapper basisUavMapper;

    @Autowired
    private BasisAntennaMapper basisAntennaMapper;

    @Autowired
    private BasisAntennaTypeMapper basisAntennaTypeMapper;

    @Autowired
    private UserDataMapper userDataMapper;

    @Autowired
    private BasisEquipmentInformationvMapper basisEquipmentInformationvMapper;

    @Autowired
    private BaseEqtypeInfoMapper baseEqtypeInfoMapper;

    @Autowired
    private BasisCaseLoadingTaskMapper basisCaseLoadingTaskMapper;

    @Autowired
    private OnlineUserService onlineUserService;

    @Autowired
    private BasisUavTableMapper basisUavTableMapper;

    @Autowired
    private BasisFrequencyConverterMapper basisFrequencyConverterMapper;
    @Autowired
    private BasisReceiverMapper basisReceiverMapper;

    @Autowired
    private BaseResourceInfoMapper baseResourceInfoMapper;

    /**
     * 查询案例加载
     * 
     * @param id 案例加载主键
     * @return 案例加载
     */
    @Override
    public BasisCaseLoading selectBasisCaseLoadingById(String id)
    {
        return basisCaseLoadingMapper.selectBasisCaseLoadingById(id);
    }

    /**
     * 查询案例加载列表
     * 
     * @param basisCaseLoading 案例加载
     * @return 案例加载
     */
    @Override
    public List<BasisCaseLoading> selectBasisCaseLoadingList(BasisCaseLoading basisCaseLoading)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        basisCaseLoading.setUserId(user.getUserId());
        return basisCaseLoadingMapper.selectBasisCaseLoadingList(basisCaseLoading);
    }

    /**
     * 新增案例加载
     * 
     * @param basisCaseLoading 案例加载
     * @return 结果
     */
    @Override
    public int insertBasisCaseLoading(BasisCaseLoading basisCaseLoading)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        basisCaseLoading.setUserId(user.getUserId());
        basisCaseLoading.setCareateTime(new Date());
        int i = basisCaseLoadingMapper.insertBasisCaseLoading(basisCaseLoading);

        for (int j = 1; j <= 3; j++) {
            basisCampMapper.addLoadingAndCamp(basisCaseLoading.getId(), j);
        }


        return i;
    }

    /**
     * 修改案例加载
     * 
     * @param basisCaseLoading 案例加载
     * @return 结果
     */
    @Override
    public int updateBasisCaseLoading(BasisCaseLoading basisCaseLoading)
    {
        return basisCaseLoadingMapper.updateBasisCaseLoading(basisCaseLoading);
    }

    /**
     * 批量删除案例加载
     * 
     * @param ids 需要删除的案例加载主键
     * @return 结果
     */
    @Override
    public int deleteBasisCaseLoadingByIds(String[] ids)
    {
        return basisCaseLoadingMapper.deleteBasisCaseLoadingByIds(ids);
    }

    /**
     * 删除案例加载信息
     * 
     * @param id 案例加载主键
     * @return 结果
     */
    @Override
    public int deleteBasisCaseLoadingById(String id)
    {
        return basisCaseLoadingMapper.deleteBasisCaseLoadingById(id);
    }

    /**
     * 根据案例查询阵地信息
     *
     * @param id 案例加载主键
     * @return 结果
     */
    @Override
    public BasisCaseLoading findPosition(String id) {

        BasisCaseLoading basisCaseLoading = basisCaseLoadingMapper.selectBasisCaseLoadingById(id);

        List<BasisCaseLoadingTask> tasks = basisCaseLoadingTaskMapper.findByCaseLoadingId(basisCaseLoading.getId());

        if (basisCaseLoading.getTaskChildren() == null){
            basisCaseLoading.setTaskChildren(new ArrayList<>());
        }

        if (basisCaseLoading.getChildren() == null){
            basisCaseLoading.setChildren(new ArrayList<>());

        }

        basisCaseLoading.getTaskChildren().addAll(tasks);

        List<BasisCamp> byLoading = basisCampMapper.findAll();
        //添加阵营  //不加入白方
        basisCaseLoading.getChildren().add(byLoading.get(0));
        basisCaseLoading.getChildren().add(byLoading.get(1));
        //遍历阵营查询阵地
        for (BasisCamp basisCamp : byLoading) {
            //根据案例id和阵营id 查询对应的阵地
            List<BasisPosition> basisPositions = basisPositionMapper.findByLoadingIdAndCampId(basisCaseLoading.getId(), Math.toIntExact(basisCamp.getCampId()) + "");

            if (!basisPositions.isEmpty() && basisCamp.getCampId() == 1){

                for (BasisPosition basisPosition : basisPositions) {
                    basisPosition.setDetectionRange("500");
                }

                //添加阵地
                if(basisCamp.getChildren() == null){
                    basisCamp.setChildren(new ArrayList<>());
                }
                basisCamp.getChildren().addAll(basisPositions);

                //根据阵地id查询天线
                for (BasisPosition basisPosition : basisPositions) {
                    if(basisPosition.getChildren() == null){
                        basisPosition.setChildren(new ArrayList<>());
                    }
                    List<Map<String, Object>> shebeiList = new ArrayList<>();
                    Map<String, Object> shebeiMap = new HashMap<>();

                    List<Object> childrenList = new ArrayList<>();
                    List<BasisEquipmentInformationv> equipmentInformationvs = basisEquipmentInformationvMapper.findByPositionIdType(Math.toIntExact(basisPosition.getId()), basisCaseLoading.getId());
                    List<BasisFrequencyConverter> byLoadingIdAndPositionId = basisFrequencyConverterMapper.findByLoadingIdAndPositionId(basisCaseLoading.getId(), basisPosition.getId() + "");
                    List<BasisReceiver> byLoadingIdAndPositionId1 = basisReceiverMapper.findByLoadingIdAndPositionId(basisCaseLoading.getId(), basisPosition.getId() + "");
                    List<BaseResourceInfo> byLoadingIdAndPositionIdFromResource = baseResourceInfoMapper.findByLoadingIdAndPositionIdFromResource(basisCaseLoading.getId(), Math.toIntExact(basisPosition.getId()));


                            if(!byLoadingIdAndPositionIdFromResource.isEmpty()){
                                for (BaseResourceInfo baseResourceInfo : byLoadingIdAndPositionIdFromResource) {
                                    baseResourceInfo.setLongitude(basisPosition.getLongitude());
                                    baseResourceInfo.setAltitude(basisPosition.getAltitude());
                                    baseResourceInfo.setLatitude(basisPosition.getLatitude());
                                }
                                shebeiMap.put("name","设备");
                                shebeiMap.put("value",0);
                                shebeiMap.put("children",byLoadingIdAndPositionIdFromResource);
                                shebeiList.add(shebeiMap);
                                basisPosition.getChildren().addAll(shebeiList);
                            }
                    //旧的
//                    if (!equipmentInformationvs.isEmpty()||!byLoadingIdAndPositionId.isEmpty() || !byLoadingIdAndPositionId1.isEmpty()){
//
//
//
//                    for (BasisEquipmentInformationv equipmentInformationv : equipmentInformationvs) {
//
//                        equipmentInformationv.setLongitude(basisPosition.getLongitude());
//                        equipmentInformationv.setAltitude(basisPosition.getAltitude());
//                        equipmentInformationv.setLatitude(basisPosition.getLatitude());
//
//                        if (equipmentInformationv.getEqInfoChildren() == null){
//                            equipmentInformationv.setEqInfoChildren(new ArrayList<>());
//                        }
//
//                        if (equipmentInformationv.getEqTypeChildren() == null){
//                            equipmentInformationv.setEqTypeChildren(new ArrayList<>());
//                        }
//
//                        if (equipmentInformationv.getUserChildren() == null){
//                            equipmentInformationv.setUserChildren(new ArrayList<>());
//                        }
//
//                        BaseEqInfo baseEqInfo = baseEqInfoMapper.selectBaseEqInfoById(equipmentInformationv.getId());
//
//                        BaseEqtypeInfo baseEqtypeInfo = baseEqtypeInfoMapper.findByEqId(equipmentInformationv.getEquipmentType());
//
//                        List<UserData> byBasisPositionEquipmentUser = userDataMapper.findByBasisPositionEquipmentUser(basisCaseLoading.getId(), basisPosition.getId(), equipmentInformationv.getId());
//
//                        equipmentInformationv.getEqTypeChildren().add(baseEqtypeInfo);
//                        equipmentInformationv.getEqInfoChildren().add(baseEqInfo);
//                        equipmentInformationv.getUserChildren().addAll(byBasisPositionEquipmentUser);
//
//
//                        childrenList.add(equipmentInformationv);
//
//
//                    }
//
//
//                        if (byLoadingIdAndPositionId.size() > 0){
//                            for (BasisFrequencyConverter basisFrequencyConverter : byLoadingIdAndPositionId) {
//                                basisFrequencyConverter.setLongitude(basisPosition.getLongitude());
//                                basisFrequencyConverter.setAltitude(basisPosition.getAltitude());
//                                basisFrequencyConverter.setLatitude(basisPosition.getLatitude());
//
//                                childrenList.add(basisFrequencyConverter);
//                            }
//                        }
//
//                        if (byLoadingIdAndPositionId1.size() > 0){
//                            for (BasisReceiver basisReceiver : byLoadingIdAndPositionId1) {
//                                basisReceiver.setLongitude(basisPosition.getLongitude());
//                                basisReceiver.setAltitude(basisPosition.getAltitude());
//                                basisReceiver.setLatitude(basisPosition.getLatitude());
//                                childrenList.add(basisReceiver);
//                            }
//                        }
//
//
//                        shebeiMap.put("name","设备");
//                        shebeiMap.put("value",0);
//                        shebeiMap.put("children",childrenList);
//                        shebeiList.add(shebeiMap);
//                        basisPosition.getChildren().addAll(shebeiList);
//                }



                    List<UserData> userDatas = userDataMapper.findByPositionId(basisPosition.getId(), basisCaseLoading.getId());

                    if (!userDatas.isEmpty()){
                        for (UserData userData : userDatas) {
                            userData.setLongitude(basisPosition.getLongitude());
                            userData.setAltitude(basisPosition.getAltitude());
                            userData.setLatitude(basisPosition.getLatitude());

                            if (userData.getRoleId() == 1){
                                userData.setName(userData.getName()+"  (指挥员)");
                            }else if(userData.getRoleId() == 0){
                                userData.setName(userData.getName()+"  (侦察员)");
                            }

                            boolean userOnline = onlineUserService.isUserOnline(userData.getUserId());
                            if (userOnline){
                                userData.setOnLine(1);
                            }else{
                                userData.setOnLine(0);

                            }
                        }


                        List<Map<String, Object>> userList = new ArrayList<>();
                        Map<String, Object> userMap = new HashMap<>();

                        userMap.put("name","人员");
                        userMap.put("value",0);
                        userMap.put("children",userDatas);
                        userList.add(userMap);
                        //添加人员
                        basisPosition.getChildren().addAll(userList);
                    }

                }
            }
            if (!basisPositions.isEmpty() && basisCamp.getCampId() == 2) {
                for (BasisPosition basisPosition : basisPositions) {
                    basisPosition.setDetectionRange("500");
                    basisPosition.setType("蓝方阵地");
                }

                //添加阵地
                if (basisCamp.getChildren() == null) {
                    basisCamp.setChildren(new ArrayList<>());
                }
                basisCamp.getChildren().addAll(basisPositions);


            }


                    List<BasisLoadingUavCamp> byLoadingIdAndCampId = basisUavMapper.findByLoadingIdAndCampId2(basisCaseLoading.getId(), Math.toIntExact(basisCamp.getCampId()));
                    for (BasisLoadingUavCamp basisLoadingUavCamp : byLoadingIdAndCampId) {
                        basisLoadingUavCamp.setDetectionRange("500");

                        if (basisLoadingUavCamp.getUavTabls() == null){
                            basisLoadingUavCamp.setUavTabls(new ArrayList<>());
                        }
                        List<BasisUavTable> byLoadingIdAndUavIdfFormUavTable = basisUavTableMapper.findByLoadingIdAndUavIdfFormUavTable(basisCaseLoading.getId(), basisLoadingUavCamp.getUavId());
                        if (byLoadingIdAndUavIdfFormUavTable.size() > 0){
                            for (BasisUavTable basisUavTable : byLoadingIdAndUavIdfFormUavTable) {
                                basisLoadingUavCamp.getUavTabls().add(basisUavTable);

                            }
                        }
                    }
            if(basisCamp.getChildren() == null){
                basisCamp.setChildren(new ArrayList<>());
            }
            basisCamp.getChildren().addAll(byLoadingIdAndCampId);





//            }


        }



        return basisCaseLoading;
    }





    @Override
    public BasisCaseLoading findPositionXueyuan(String id,String userId) {
        BasisCaseLoading basisCaseLoading = basisCaseLoadingMapper.selectBasisCaseLoadingById(id);

        List<BasisCaseLoadingTask> tasks = basisCaseLoadingTaskMapper.findByCaseLoadingId(basisCaseLoading.getId());

        if (basisCaseLoading.getTaskChildren() == null){
            basisCaseLoading.setTaskChildren(new ArrayList<>());
        }

        if (basisCaseLoading.getChildren() == null){
            basisCaseLoading.setChildren(new ArrayList<>());

        }

        basisCaseLoading.getTaskChildren().addAll(tasks);

        List<BasisCamp> byLoading = basisCampMapper.findAll();
        //添加阵营  //不加入白方
        basisCaseLoading.getChildren().add(byLoading.get(0));
        basisCaseLoading.getChildren().add(byLoading.get(1));

//        basisCaseLoading.getChildren().addAll(byLoading);
        //遍历阵营查询阵地
        for (BasisCamp basisCamp : byLoading) {
            //根据案例id和人员 查询对应的阵地
            List<BasisPosition> basisPositions = basisPositionMapper.findByLoadingIdAndCampIdFormUser(basisCaseLoading.getId(), userId,Math.toIntExact(basisCamp.getCampId()) + "");

            if (!basisPositions.isEmpty()){
                //添加阵地
                if(basisCamp.getChildren() == null){
                    basisCamp.setChildren(new ArrayList<>());
                }
                basisCamp.getChildren().addAll(basisPositions);

                //根据阵地id查询天线
                for (BasisPosition basisPosition : basisPositions) {
                    if(basisPosition.getChildren() == null){
                        basisPosition.setChildren(new ArrayList<>());
                    }

                    List<Map<String, Object>> shebeiList = new ArrayList<>();
                    Map<String, Object> shebeiMap = new HashMap<>();

                    List<BasisEquipmentInformationv> equipmentInformationvs = basisEquipmentInformationvMapper.findByPositionIdTypeXueyuan(Math.toIntExact(basisPosition.getId()), basisCaseLoading.getId(),userId);

                    if (!equipmentInformationvs.isEmpty()){
                        shebeiMap.put("name","设备");
                        shebeiMap.put("value",0);
                        shebeiMap.put("children",equipmentInformationvs);
                        shebeiList.add(shebeiMap);


                        basisPosition.getChildren().addAll(shebeiList);

                        for (BasisEquipmentInformationv equipmentInformationv : equipmentInformationvs) {

                            equipmentInformationv.setLongitude(basisPosition.getLongitude());
                            equipmentInformationv.setAltitude(basisPosition.getAltitude());
                            equipmentInformationv.setLatitude(basisPosition.getLatitude());

                            if (equipmentInformationv.getEqInfoChildren() == null){
                                equipmentInformationv.setEqInfoChildren(new ArrayList<>());
                            }

                            if (equipmentInformationv.getEqTypeChildren() == null){
                                equipmentInformationv.setEqTypeChildren(new ArrayList<>());
                            }

                            if (equipmentInformationv.getUserChildren() == null){
                                equipmentInformationv.setUserChildren(new ArrayList<>());
                            }

                            BaseEqInfo baseEqInfo = baseEqInfoMapper.selectBaseEqInfoById(equipmentInformationv.getId());

                            BaseEqtypeInfo baseEqtypeInfo = baseEqtypeInfoMapper.findByEqId(equipmentInformationv.getEquipmentType());

                            List<UserData> byBasisPositionEquipmentUser = userDataMapper.findByBasisPositionEquipmentUser(basisCaseLoading.getId(), basisPosition.getId(), equipmentInformationv.getId());

                            equipmentInformationv.getEqTypeChildren().add(baseEqtypeInfo);
                            equipmentInformationv.getEqInfoChildren().add(baseEqInfo);
                            equipmentInformationv.getUserChildren().addAll(byBasisPositionEquipmentUser);


                        }
                    }






                    List<UserData> userDatas = userDataMapper.findByPositionIdXueyuan(basisPosition.getId(), basisCaseLoading.getId(),userId);

                    if (!userDatas.isEmpty()){
                        for (UserData userData : userDatas) {
                            userData.setLongitude(basisPosition.getLongitude());
                            userData.setAltitude(basisPosition.getAltitude());
                            userData.setLatitude(basisPosition.getLatitude());

                            boolean userOnline = onlineUserService.isUserOnline(userData.getUserId());
                            if (userOnline){
                                userData.setOnLine(1);
                            }else{
                                userData.setOnLine(0);

                            }
                        }


                        List<Map<String, Object>> userList = new ArrayList<>();
                        Map<String, Object> userMap = new HashMap<>();

                        userMap.put("name","人员");
                        userMap.put("value",0);
                        userMap.put("children",userDatas);
                        userList.add(userMap);
                        //添加人员
                        basisPosition.getChildren().addAll(userList);
                    }

                }
            }
//            List<BasisLoadingUavCamp> byLoadingIdAndCampId = basisUavMapper.findByLoadingIdAndCampId(basisCaseLoading.getId(), Math.toIntExact(basisCamp.getCampId()));
//
//            for (BasisLoadingUavCamp basisLoadingUavCamp : byLoadingIdAndCampId) {
//                if (basisLoadingUavCamp.getUavTabls() == null){
//                    basisLoadingUavCamp.setUavTabls(new ArrayList<>());
//                }
//                List<BasisUavTable> byLoadingIdAndUavIdfFormUavTable = basisUavTableMapper.findByLoadingIdAndUavIdfFormUavTable(basisCaseLoading.getId(), basisLoadingUavCamp.getUavId());
//                if (byLoadingIdAndUavIdfFormUavTable.size() > 0){
//                    for (BasisUavTable basisUavTable : byLoadingIdAndUavIdfFormUavTable) {
//                        basisLoadingUavCamp.getUavTabls().add(basisUavTable);
//
//                    }
//                }
//            }
//
//            if(basisCamp.getChildren() == null){
//                basisCamp.setChildren(new ArrayList<>());
//            }
//            basisCamp.getChildren().addAll(byLoadingIdAndCampId);
        }



        return basisCaseLoading;
    }



    @Override
    public BasisCaseLoading findPositionDT(String id) {

        BasisCaseLoading basisCaseLoading = basisCaseLoadingMapper.selectBasisCaseLoadingById(id);

        List<BasisCaseLoadingTask> tasks = basisCaseLoadingTaskMapper.findByCaseLoadingId(basisCaseLoading.getId());

        if (basisCaseLoading.getTaskChildren() == null){
            basisCaseLoading.setTaskChildren(new ArrayList<>());
        }

        if (basisCaseLoading.getChildren() == null){
            basisCaseLoading.setChildren(new ArrayList<>());

        }

        basisCaseLoading.getTaskChildren().addAll(tasks);

        List<BasisCamp> byLoading = basisCampMapper.findAll();
        //添加阵营  //不加入白方
        basisCaseLoading.getChildren().add(byLoading.get(0));
        basisCaseLoading.getChildren().add(byLoading.get(1));
        //遍历阵营查询阵地
        for (BasisCamp basisCamp : byLoading) {
            //根据案例id和阵营id 查询对应的阵地
            List<BasisPosition> basisPositions = basisPositionMapper.findByLoadingIdAndCampId(basisCaseLoading.getId(), Math.toIntExact(basisCamp.getCampId()) + "");
            if (!basisPositions.isEmpty() && basisCamp.getCampId() == 1){


                for (BasisPosition basisPosition : basisPositions) {
                    basisPosition.setDetectionRange("500");
                }
                //添加阵地
                if(basisCamp.getChildren() == null){
                    basisCamp.setChildren(new ArrayList<>());
                }
                basisCamp.getChildren().addAll(basisPositions);

                //根据阵地id查询天线
                for (BasisPosition basisPosition : basisPositions) {
                    if(basisPosition.getChildren() == null){
                        basisPosition.setChildren(new ArrayList<>());
                    }
                    List<Map<String, Object>> shebeiList = new ArrayList<>();
                    Map<String, Object> shebeiMap = new HashMap<>();

                    List<BaseResourceInfo> byLoadingIdAndPositionIdFromResource = baseResourceInfoMapper.findByLoadingIdAndPositionIdFromResource(basisCaseLoading.getId(), Math.toIntExact(basisPosition.getId()));


                    if(!byLoadingIdAndPositionIdFromResource.isEmpty()){
                        for (BaseResourceInfo baseResourceInfo : byLoadingIdAndPositionIdFromResource) {
                            baseResourceInfo.setLongitude(basisPosition.getLongitude());
                            baseResourceInfo.setAltitude(basisPosition.getAltitude());
                            baseResourceInfo.setLatitude(basisPosition.getLatitude());
                        }
                        shebeiMap.put("name","设备");
                        shebeiMap.put("value",0);
                        shebeiMap.put("children",byLoadingIdAndPositionIdFromResource);
                        shebeiList.add(shebeiMap);
                        basisPosition.getChildren().addAll(shebeiList);
                    }




                    List<UserData> userDatas = userDataMapper.findByPositionId(basisPosition.getId(), basisCaseLoading.getId());

                    if (!userDatas.isEmpty()){
                        for (UserData userData : userDatas) {
                            userData.setLongitude(basisPosition.getLongitude());
                            userData.setAltitude(basisPosition.getAltitude());
                            userData.setLatitude(basisPosition.getLatitude());

                            if (userData.getRoleId() == 1){
                                userData.setName(userData.getName()+"  (指挥员)");
                            }else if(userData.getRoleId() == 0){
                                userData.setName(userData.getName()+"  (侦察员)");
                            }

                            boolean userOnline = onlineUserService.isUserOnline(userData.getUserId());
                            if (userOnline){
                                userData.setOnLine(1);
                            }else{
                                userData.setOnLine(0);

                            }
                        }


                        List<Map<String, Object>> userList = new ArrayList<>();
                        Map<String, Object> userMap = new HashMap<>();

                        userMap.put("name","人员");
                        userMap.put("value",0);
                        userMap.put("children",userDatas);
                        userList.add(userMap);
                        //添加人员
                        basisPosition.getChildren().addAll(userList);
                    }

                }
            }
            if (!basisPositions.isEmpty() && basisCamp.getCampId() == 2) {
                for (BasisPosition basisPosition : basisPositions) {
                    basisPosition.setDetectionRange("500");
                    basisPosition.setType("蓝方阵地");
                }

                //添加阵地
                if (basisCamp.getChildren() == null) {
                    basisCamp.setChildren(new ArrayList<>());
                }
                basisCamp.getChildren().addAll(basisPositions);


            }


            List<BasisLoadingUavCamp> byLoadingIdAndCampId = basisUavMapper.findByLoadingIdAndCampIdDT2(id, Math.toIntExact(basisCamp.getCampId()));
            for (BasisLoadingUavCamp basisLoadingUavCamp : byLoadingIdAndCampId) {
                basisLoadingUavCamp.setDetectionRange("500");

                if (basisLoadingUavCamp.getUavTabls() == null){
                    basisLoadingUavCamp.setUavTabls(new ArrayList<>());
                }
                List<BasisUavTable> byLoadingIdAndUavIdfFormUavTable = basisUavTableMapper.findByLoadingIdAndUavIdfFormUavTableDT(id, basisLoadingUavCamp.getUavId());
                if (byLoadingIdAndUavIdfFormUavTable.size() > 0){
                    for (BasisUavTable basisUavTable : byLoadingIdAndUavIdfFormUavTable) {
                        basisLoadingUavCamp.getUavTabls().add(basisUavTable);

                    }
                }
            }
            if(basisCamp.getChildren() == null){
                basisCamp.setChildren(new ArrayList<>());
            }
            basisCamp.getChildren().addAll(byLoadingIdAndCampId);

        }



        return basisCaseLoading;
    }


    //查询指挥人员显示的树
    @Override
    public BasisCaseLoading findPositionZH(String id,Long userId) {

        BasisCaseLoading basisCaseLoading = basisCaseLoadingMapper.selectBasisCaseLoadingById(id);

        List<BasisCaseLoadingTask> tasks = basisCaseLoadingTaskMapper.findByCaseLoadingId(basisCaseLoading.getId());

        if (basisCaseLoading.getTaskChildren() == null){
            basisCaseLoading.setTaskChildren(new ArrayList<>());
        }

        if (basisCaseLoading.getChildren() == null){
            basisCaseLoading.setChildren(new ArrayList<>());

        }

        basisCaseLoading.getTaskChildren().addAll(tasks);

        List<BasisCamp> byLoading = basisCampMapper.findAll();
        //添加阵营  //不加入白方
        basisCaseLoading.getChildren().add(byLoading.get(0));
        //遍历阵营查询阵地
        for (BasisCamp basisCamp : byLoading) {
            //根据案例id和阵营id 查询对应的阵地
            List<BasisPosition> basisPositions = basisPositionMapper.selectByLoadingIdAnduserIdFromPosition(basisCaseLoading.getId(), userId);
            if (!basisPositions.isEmpty()){
                //添加阵地
                if(basisCamp.getChildren() == null){
                    basisCamp.setChildren(new ArrayList<>());
                }
                basisCamp.getChildren().addAll(basisPositions);

                //根据阵地id查询天线
                for (BasisPosition basisPosition : basisPositions) {
                    if(basisPosition.getChildren() == null){
                        basisPosition.setChildren(new ArrayList<>());
                    }
                    List<Map<String, Object>> shebeiList = new ArrayList<>();
                    Map<String, Object> shebeiMap = new HashMap<>();

                    List<BaseResourceInfo> byLoadingIdAndPositionIdFromResource = baseResourceInfoMapper.findByLoadingIdAndPositionIdFromResource(basisCaseLoading.getId(), Math.toIntExact(basisPosition.getId()));


                    if(!byLoadingIdAndPositionIdFromResource.isEmpty()){
                        for (BaseResourceInfo baseResourceInfo : byLoadingIdAndPositionIdFromResource) {
                            baseResourceInfo.setLongitude(basisPosition.getLongitude());
                            baseResourceInfo.setAltitude(basisPosition.getAltitude());
                            baseResourceInfo.setLatitude(basisPosition.getLatitude());
                        }
                        shebeiMap.put("name","设备");
                        shebeiMap.put("value",0);
                        shebeiMap.put("children",byLoadingIdAndPositionIdFromResource);
                        shebeiList.add(shebeiMap);
                        basisPosition.getChildren().addAll(shebeiList);
                    }




                    List<UserData> userDatas = userDataMapper.findByPositionId(basisPosition.getId(), basisCaseLoading.getId());

                    if (!userDatas.isEmpty()){
                        for (UserData userData : userDatas) {
                            userData.setLongitude(basisPosition.getLongitude());
                            userData.setAltitude(basisPosition.getAltitude());
                            userData.setLatitude(basisPosition.getLatitude());

                            if (userData.getRoleId() == 1){
                                userData.setName(userData.getName()+"  (指挥员)");
                            }else if(userData.getRoleId() == 0){
                                userData.setName(userData.getName()+"  (侦察员)");
                            }

                            boolean userOnline = onlineUserService.isUserOnline(userData.getUserId());
                            if (userOnline){
                                userData.setOnLine(1);
                            }else{
                                userData.setOnLine(0);

                            }
                        }


                        List<Map<String, Object>> userList = new ArrayList<>();
                        Map<String, Object> userMap = new HashMap<>();

                        userMap.put("name","人员");
                        userMap.put("value",0);
                        userMap.put("children",userDatas);
                        userList.add(userMap);
                        //添加人员
                        basisPosition.getChildren().addAll(userList);
                    }

                }
            }
//            List<BasisLoadingUavCamp> byLoadingIdAndCampId = basisUavMapper.findByLoadingIdAndCampIdDT(basisCaseLoading.getId(), Math.toIntExact(basisCamp.getCampId()));
//
//            for (BasisLoadingUavCamp basisLoadingUavCamp : byLoadingIdAndCampId) {
//                if (basisLoadingUavCamp.getUavTabls() == null){
//                    basisLoadingUavCamp.setUavTabls(new ArrayList<>());
//                }
//                List<BasisUavTable> byLoadingIdAndUavIdfFormUavTable = basisUavTableMapper.findByLoadingIdAndUavIdfFormUavTableDT(basisCaseLoading.getId(), basisLoadingUavCamp.getUavId());
//                if (byLoadingIdAndUavIdfFormUavTable.size() > 0){
//                    for (BasisUavTable basisUavTable : byLoadingIdAndUavIdfFormUavTable) {
//                        basisLoadingUavCamp.getUavTabls().add(basisUavTable);
//
//                    }
//                }
//            }
//
//            if(basisCamp.getChildren() == null){
//                basisCamp.setChildren(new ArrayList<>());
//            }
//            basisCamp.getChildren().addAll(byLoadingIdAndCampId);
        }



        return basisCaseLoading;
    }


    @Override
    public String copyLoading(String id, String name) {
        try{


            BasisCaseLoading basisCaseLoading = basisCaseLoadingMapper.selectBasisCaseLoadingById(id);
            String loadingId = basisCaseLoading.getId();
            basisCaseLoading.setCaseName(name);
            LocalDateTime now = LocalDateTime.now();

            // 定义日期时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");

            // 格式化当前时间
            String formattedDateTime = now.format(formatter);
            basisCaseLoading.setId("SCPC-ZD001-"+formattedDateTime);

            basisCaseLoadingMapper.insertBasisCaseLoading(basisCaseLoading);

            List<BasisCamp> byLoading = basisCampMapper.findByLoading(loadingId);
            for (BasisCamp basisCamp : byLoading) {
                basisCamp.setLoadingId(basisCaseLoading.getId());

                basisCampMapper.copyLoadingAndCamp(basisCamp);


            List<BasisPosition> basisPositions = basisPositionMapper.findByLoadingIdAndCampId(loadingId, basisCamp.getCampId()+"");
            for (BasisPosition basisPosition : basisPositions) {
                BasisLoadingPositionCamp basisLoadingPositionCamp = new BasisLoadingPositionCamp();
                basisLoadingPositionCamp.setCampId(Math.toIntExact(basisCamp.getCampId()));
                basisLoadingPositionCamp.setLoadingId(basisCaseLoading.getId());
                basisLoadingPositionCamp.setPositionId(Math.toIntExact(basisPosition.getId()));
                basisLoadingPositionCamp.setWeatherId(basisPosition.getWeatherId());
                basisPositionMapper.addBasisLoadingPositionCamp(basisLoadingPositionCamp);



                List<BaseResourceInfo> byLoadingIdAndPositionIdFromResource = baseResourceInfoMapper.findByLoadingIdAndPositionIdFromResource(loadingId, Math.toIntExact(basisPosition.getId()));

                for (BaseResourceInfo baseResourceInfo : byLoadingIdAndPositionIdFromResource) {
                    BasisPositionResourceInfo basisPositionResourceInfo = new BasisPositionResourceInfo();
                    basisPositionResourceInfo.setResourceId(Math.toIntExact(baseResourceInfo.getId()));
                    basisPositionResourceInfo.setPositionId(Math.toIntExact(basisPosition.getId()));
                    basisPositionResourceInfo.setLoadingId(basisCaseLoading.getId());

                    baseResourceInfoMapper.addBasisPositionResourceInfo(basisPositionResourceInfo);

                    List<UserData> byLoadingIdFormUserResource = userDataMapper.findByLoadingIdFormUserResource(basisCaseLoading.getId());

                    for (UserData userData : byLoadingIdFormUserResource) {
                        BasisPositionResourceInfoUser basisPositionResourceInfoUser = new BasisPositionResourceInfoUser();
                        basisPositionResourceInfoUser.setUserId(userData.getUserId());
                        basisPositionResourceInfoUser.setLoadingId(basisCaseLoading.getId());
                        basisPositionResourceInfoUser.setPositionId(Math.toIntExact(basisPosition.getId()));
                        basisPositionResourceInfoUser.setResourceId(Math.toIntExact(baseResourceInfo.getId()));
                        userDataMapper.addBasisLoadingPositionResourcesUser(basisPositionResourceInfoUser);
                    }
                }

                List<UserData> userDatas = userDataMapper.findByPositionId(basisPosition.getId(), loadingId);
                for (UserData userData : userDatas) {
                    userData.setLoadingId(basisCaseLoading.getId());
                    userDataMapper.addBasisPositionUser(userData);
                }




            }
            }

            List<BasisLoadingUavCamp> byLoadingIdAndCampId = basisUavMapper.findByLoadingIdAndCampId2(loadingId, 2);
            if (byLoadingIdAndCampId.size() > 0 ){
                for (BasisLoadingUavCamp basisLoadingUavCamp : byLoadingIdAndCampId) {
                    int uavId = basisLoadingUavCamp.getUavId();

                            basisLoadingUavCamp.setLoadingId(basisCaseLoading.getId());
                    basisUavMapper.addBasisLoadingUavCamp(basisLoadingUavCamp);

                    List<BasisUavTable> byLoadingIdAndUavIdfFormUavTable = basisUavTableMapper.findByLoadingIdAndUavIdfFormUavTableDT(loadingId, uavId);
                    if (byLoadingIdAndUavIdfFormUavTable.size() > 0){
                        for (BasisUavTable basisUavTable : byLoadingIdAndUavIdfFormUavTable) {
                            basisUavTable.setLoadingId(basisCaseLoading.getId());
                            basisUavTable.setUavId(Long.valueOf(basisLoadingUavCamp.getUavId()));
                            basisUavTableMapper.insertBasisUavTable(basisUavTable);
                        }
                    }

                }
            }

            return "复制成功";

        }catch (Exception e){
            return "复制失败";
        }
    }

    @Override
    public boolean configBreakpointTime(String loadingId, String breakpointTime) {
        return basisCaseLoadingMapper.configBreakpointTime(loadingId, breakpointTime);
    }

    @Override
    public List<BasisCaseLoading> findByUsuerRoleIdFromloading(Integer userId) {
        return basisCaseLoadingMapper.findByUsuerRoleIdFromloading(userId);
    }
}
